package com.base.util;


import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.RC2ParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Xuyh on 2016-12-02.
 */
public class RC2 {
    public static String encrypt(byte[] key, byte[] iv, String unencrypted) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException{
        RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
        Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
        byte[] encrypted = cipher.doFinal(unencrypted.getBytes());
        return DatatypeConverter.printBase64Binary(encrypted);
    }

    public static String decrypt(byte[] key, byte[] iv, String encrypted) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException {
        RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
        Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
        byte[] decrypted = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted));
        return new String(decrypted);
    }

    public static  String getRealPass(String pass){
        String realpass = "";
        byte[] key = Base64Helper.decode("acgvf84GSDZAXeiquyyr+Q==");
        byte[] iv = Base64Helper.decode("qI83PLKIk+Q=");
        try {
            realpass = encrypt(key,iv,pass);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return realpass;
    }

    public static void main(String args[]){
        byte[] key = Base64Helper.decode("acgvf84GSDZAXeiquyyr+Q==");
        byte[] iv = Base64Helper.decode("qI83PLKIk+Q=");
        try {
            String info = encrypt(key,iv,"123456");
            System.out.println(info);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
    }
}